using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DDF.Data;
using DDF.Data.Providers;
using System.Data;
using System.Linq.Expressions;
using System.Configuration;
using ARR.Entities.Gateways;

namespace ARR.DataModel.Repositories
{
	public static partial class TempArticleAttributesRepository
	{
		private static string connString = ConfigurationManager.ConnectionStrings["Default"].ConnectionString;
		
		public class TempArticleAttributesDbo : DatabaseObject
        {
            public TempArticleAttributesDbo()
            {
                Name = "TempArticleAttributes";
                GeneratedClassName = "TempArticleAttributes";
                SequenceName = "N/A";
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Id", ColumnType = "int", IsIdentity = true, IsNullable = false, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "Id" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Code", ColumnType = "varchar", IsIdentity = false, IsNullable = false, Length = 20, Precision = 0, Scale = 0, GeneratedPropertyName = "Code" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "UOM", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 50, Precision = 0, Scale = 0, GeneratedPropertyName = "UOM" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "AttributeId", ColumnType = "int", IsIdentity = false, IsNullable = false, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "AttributeId" });
              
            }
        }
		
		public static List<TempArticleAttributeRow> Select(Expression<Func<TempArticleAttributeRow, bool>> expression)
        {
            List<TempArticleAttributeRow> rows = new List<TempArticleAttributeRow>();

            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                IDataReader reader = database.Select<TempArticleAttributeRow>(new TempArticleAttributesDbo(), expression);

                Load(reader, rows);
            }

            return rows;
        }
		
		public static List<TempArticleAttributeRow> Select()
		{
			List<TempArticleAttributeRow> rows = new List<TempArticleAttributeRow>();

			using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
			{
				IDataReader reader = database.Select<TempArticleAttributeRow>(new TempArticleAttributesDbo(), null);

				Load(reader, rows);
			}

			return rows;
		}
		
		public static List<TempArticleAttributeRow> PagedSelect(Expression<Func<TempArticleAttributeRow, bool>> filterExpression, Expression<Func<TempArticleAttributeRow, object>> orderByExpression, int pageNumber, int pageSize)
		{
			List<TempArticleAttributeRow> rows = new List<TempArticleAttributeRow>();
			
			using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
			{
				IDataReader reader = database.PagedSelect<TempArticleAttributeRow>(new TempArticleAttributesDbo(), filterExpression, orderByExpression, pageNumber, pageSize);

				Load(reader, rows);
			}

			return rows;
		}
		
		private static void Load(IDataReader reader, List<TempArticleAttributeRow> TempArticleAttributeList)
        {
            TempArticleAttributeRow row;

            while (reader.Read())
            {
                row = new TempArticleAttributeRow()
                {
					Id = Util.CastDbValue<int>(reader["Id"]),
					Code = Util.CastDbValue<string>(reader["Code"]),
					UOM = Util.CastDbValue<string>(reader["UOM"]),
					AttributeId = Util.CastDbValue<int>(reader["AttributeId"])
                };
                                
                TempArticleAttributeList.Add(row);
            }

            reader.Close();
        }

			
		public static void Insert(TempArticleAttributeRow row)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Insert<TempArticleAttributeRow>(new TempArticleAttributesDbo(), row);
            }
        }

		public static void Update(TempArticleAttributeRow row)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Update<TempArticleAttributeRow>(new TempArticleAttributesDbo(), row);
            }
        }

		public static void Delete(Expression<Func<TempArticleAttributeRow, bool>> expression)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Delete<TempArticleAttributeRow>(new TempArticleAttributesDbo(), expression);
            }
        }

	}
}
	
